package com.darrenchan.netease03

object ApplyTest {
  def main(args: Array[String]): Unit = {
    //def apply(x : scala.Int, xs : scala.Int*)
    //调用apply方法
    val array1 = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    //调用构造器
    val array2 = new Array[Int](9)

    println(array1.toBuffer)
    println(array2.toBuffer)


    //    Error:(16, 15) constructor Student44 in class Student44 cannot be accessed in object ApplyTest
    //    val stu = new Student44(111, "cc") //报错


    //这是因为在伴生对象中，定义了apply方法，所以该方式可以正常得到一个Student44对象
    val stu = Student44(111, "cc")

    println(stu.id + ", " + stu.name)
  }
}

//主构造器，被private修饰以后，其实这个主构造器，就只能在对应的伴生对象中才能使用
class Student44 private(val id: Int, val name: String) {

}

object Student44 {
  //apply方法的目的就是为了创建一个Student44对象
  //方式：val stu = Student44(111, "cc")
  def apply(id: Int, name: String) = {
    println("调用了apply方法")
    new Student44(id, name)
  }

  //apply方法可以重载
}
